<?php

namespace app\nav\service;

use app\nav\model\NavCate;
use app\nav\model\NavLinks;
use think\facade\Cache;

class MenuService
{

    /**
     * 生成带单信息
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function getMenus()
    {
        $obj = array();

        //首页信息
        $obj['homeInfo'] = [
            'title' => '',
            'icon'  => 'fa fa-home',
            'href'  => sysconf('app_index') | 'http://fk.wandhi.com'
        ];
        //logo信息
        $obj["logoInfo"] = [
            "title" => sysconf('site_name'),
            "image" => "logo.png",
            "href"  => ""
        ];

        $obj['menuInfo'] = $this->initMenus();
        return $obj;
    }

    private function initMenus($istools = 0)
    {
        $cates = array();
        foreach (NavCate::where('istools', $istools)->order('sort')->select()->toArray() as $item) {
            $cates[$item['id']] = $item;
        }

        foreach (NavLinks::order('sort')->all()->toArray() as $item) {
            if (array_key_exists($item['cate'], $cates)) {
                $cates[$item['cate']]['child'][] = $item;
            }
        }
        return $cates;
    }

    public function clear()
    {
        Cache::clear('index');
    }

}